<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('服务端报文映射列表')"/>
</head>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">
        <form action="" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">请求渠道号</label>
                <div class="layui-input-inline">
                    <select dict-code="msg_channel_no" name="reqChannelNo">
                        <option value="">请选择</option>
                    </select>
                </div>
                <label class="layui-form-label">请求报文名称</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="reqMsgName" type="text">
                </div>
                <label class="layui-form-label">响应渠道号</label>
                <div class="layui-input-inline">
                    <select dict-code="msg_channel_no" name="respChannelNo">
                        <option value="">请选择</option>
                    </select>
                </div>
                <label class="layui-form-label">响应报文名称</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="respMsgName" type="text">
                </div>
            </div>
            <div class="layui-form-item">
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-filter="msg-query" lay-submit>
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button class="pear-btn pear-btn-md" type="reset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="msg-table" lay-filter="msg-table"></table>
    </div>
</div>
</body>

<script id="msg-toolbar" type="text/html">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script id="user-bar" type="text/html">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script id="user-reqChannelNo" type="text/html">
    {{layui.dictionary.parseDictText(d.reqChannelNo, 'msg_channel_no')}}
</script>

<script id="user-respChannelNo" type="text/html">
    {{layui.dictionary.parseDictText(d.respChannelNo, 'msg_channel_no')}}
</script>

<script id="user-createTime" type="text/html">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
</script>

<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'popup', 'dictionary'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let popup = layui.popup;

        let MODULE_PATH = "/sim/msgCode/mapping/";

        let cols = [
            [
                {type: 'checkbox'},
                {title: '请求渠道号', field: 'reqChannelNo', align: 'center', templet: '#user-reqChannelNo'},
                {title: '请求报文编号', field: 'reqMsgCode', align: 'center'},
                {title: '请求报文名称', field: 'reqMsgName', align: 'center'},
                {title: '响应渠道号', field: 'respChannelNo', align: 'center', templet: '#user-respChannelNo'},
                {title: '响应报文编号', field: 'respMsgCode', align: 'center'},
                {title: '响应报文名称', field: 'respMsgName', align: 'center'},
                {title: '描述', field: 'remark', align: 'center'},
                {title: '创建时间', field: 'createTime', align: 'center', templet: '#user-createTime'},
                {title: '操作', toolbar: '#user-bar', align: 'center', width: 120}
            ]
        ]

        table.render({
            elem: '#msg-table',
            url: MODULE_PATH + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#msg-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(msg-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(msg-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(msg-query)', function (data) {
            window.refresh(data.field);
            return false;
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['60%', '100%'],
                content: MODULE_PATH + 'add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['60%', '100%'],
                content: MODULE_PATH + 'edit?id=' + obj.data['id']
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该数据', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            popup.success(result.msg, function () {
                                table.reload('msg-table');
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let ids = common.checkField(obj, 'id');
            if (common.isEmpty(ids)) {
                popup.warning("未选中数据");
                return false;
            }
            layer.confirm('确定要删除选中数据', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "batchRemove/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            popup.success(result.msg, function () {
                                table.reload('msg-table');
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                });
            });
        }

        window.refresh = function (param) {
            table.reload('msg-table', {where: param});
        }
    })
</script>
</html>